#include <opencv2/core/core.hpp>
#include <opencv2/video/background_segm.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <stdio.h>
#include <stdlib.h>

#include <string.h>
#include <iostream>
#include <fstream>
#include <ctime>
#include <sstream>
#include <string>
#include <unistd.h>

using namespace cv;
using namespace std;

void getImage();
void getImage();
void saveImage();
int compareImage();
void updateImages();
void editLog();

#define THRESHOLD 15
#define DEBUG printf("LINE: %d\n",__LINE__);

Mat newImage,  HSV_oldImage, HSV_procImage, oldImage;
String s;
int runCounter = 0;
float compareValue;
bool imageSaved;

VideoCapture cap(0);
FILE *runRead;
FILE *runSave;
int main() {
	runRead = fopen("run.txt","r");
	if(!runRead)printf("FNF\n");
	else
	{
		fscanf(runRead,"%d",&runCounter);
		fclose(runRead);
	}
	//SETUP THE IMAGE AND CAMERA
	char inputKey = waitKey(1);
	cap.set(CV_CAP_PROP_FRAME_WIDTH, 1920);
	cap.set(CV_CAP_PROP_FRAME_HEIGHT, 1080);
	namedWindow("Proc Image",CV_WINDOW_NORMAL);
	while(1){
		//DEBUG
		
		//DEBUG
		if(!cap.grab())printf("Grab failed\n");		
		getImage();
		//imshow("Proc Image",newImage);
		//DEBUG
		updateImages();
		
		if (compareImage()){
			saveImage();
		}
		
		imshow("Proc Image",HSV_procImage);
	//imshow("old.jpg",HSV_oldImage);
	//imshow("dif",diff_im);
		editLog();
		runCounter++;
		sleep(10);

	}
	cap.release();
	return 1;
}
void getImage(){
	cap >> newImage;
}
int compareImage(){
	GaussianBlur(HSV_procImage,HSV_procImage,Size(3,3),0,0,BORDER_DEFAULT);
	GaussianBlur(HSV_oldImage,HSV_oldImage,Size(3,3),0,0,BORDER_DEFAULT);
	Mat diff_im = HSV_procImage - HSV_oldImage;
	imwrite("proc.jpg",HSV_procImage);
	imwrite("old.jpg",HSV_oldImage);
	int sum = 0;
	for(int y=0;y<diff_im.rows;y++)
    	{
      		for(int x=0;x<diff_im.rows;x++)
       		{
			//printf("Diff img = (%d,%d)",diff_im.rows,diff_im.rows);
			//printf("Value at %d,%d is %d\n",x,y,diff_im.at<uchar>(Point(x,y)));
			if(diff_im.at<uchar>(Point(x,y))>20) diff_im.at<uchar>(Point(x,y)) = 255;
			sum = sum + diff_im.at<uchar>(Point(x,y));
		}
	}
	imwrite("dif.jpg",diff_im);
	sum = abs(sum);
	printf("Sum: %d\n",sum);
	float percSum = (sum/((diff_im.rows*255.)*(diff_im.cols*255.)))*100;
	if( sum < 600){
		imageSaved = true;
		printf("Diff & compare value: %f\n", percSum);
		return 1;

	}
	else{
		printf("Same & compare value: %f\n", percSum);
		imageSaved = false;
		return 0;

	}

}
void saveImage(){

	stringstream ss;
	ss << "image" << runCounter << ".jpg";
	s = ss.str();
	imwrite(s, newImage);

}
void updateImages(){
	if(runCounter==0){
		cvtColor(newImage, HSV_procImage, CV_RGB2GRAY );
		cvtColor(newImage, HSV_oldImage, CV_RGB2GRAY );
	}
	else{
		HSV_oldImage = HSV_procImage.clone();
		cvtColor(newImage, HSV_procImage, CV_RGB2GRAY );
		
	}
}
void editLog(){
	ofstream myfile;
	time_t rawtime;
	struct tm * timeinfo;
	time ( &rawtime );
	timeinfo = localtime ( &rawtime );
	myfile.open("log.csv", ios::app | ios::out | ios::binary);
	myfile << "Run Counter: "<< runCounter << ",";
	myfile << "Compare: " << compareValue << ",";
	myfile << "Image is Saved: " << imageSaved << ",";
	myfile << asctime(timeinfo);
	myfile.close();
	
	runSave = fopen("run.txt","w");
	fprintf(runSave,"%d",runCounter);
	fclose(runSave);
}


